package com.yljk.yljkproject.modelcallback;

import android.content.Context;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMCustomMessageBody;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.yilijk.base.utils.ALog;
import com.yljk.servicemanager.constants.RouterApi;
import com.yljk.servicemanager.interfaceapi.IMBackApi;
import com.yljk.yljkproject.utils.ReactUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * create by shicheng
 *
 * 消息模块回调实现
 * message module interface callback
 *
 */
@Route(path = RouterApi.IMONCLICKIMPL)
public
class IMBackImpl implements IMBackApi {

    @Override
    public void push(Context context, String pluginId, String componentName) {
        ReactUtils.INSTANCE.push(context, pluginId, componentName);
    }

    @Override
    public void push(Context context, String pluginId, String componentName, Bundle bundle) {
        ReactUtils.INSTANCE.push(context, pluginId, componentName,bundle);
    }

    @Override
    public List<Map<String, Object>> getAllConversation() {
        List<Map<String, Object>> lists = new ArrayList<>();

        Map<String, EMConversation> allConversations = EMClient.getInstance().chatManager().getAllConversations();
        Set<Map.Entry<String, EMConversation>> entries = allConversations.entrySet();
            for (Map.Entry<String, EMConversation> e : entries
            ) {
                HashMap<String, Object> emap = new HashMap<>();
                emap.put("to",e.getValue().getLastMessage().getTo());
                ALog.d("toto", "onSuccess: "+e.getValue().getLastMessage().getTo());
                emap.put("sendTime",e.getValue().getLastMessage().getMsgTime());
                emap.put("unreadMsgCount",e.getValue().getUnreadMsgCount());

                if (e.getValue().getLastMessage().getType() == EMMessage.Type.TXT){
                    EMTextMessageBody body = (EMTextMessageBody) e.getValue().getLastMessage().getBody();
                    emap.put("content",body.getMessage());
                    emap.put("msgType","TXT");
                }else if (e.getValue().getLastMessage().getType() == EMMessage.Type.VOICE){
                    emap.put("content","audio");
                }else if (e.getValue().getLastMessage().getType() == EMMessage.Type.IMAGE){
                    emap.put("content","img");
                }else if (e.getValue().getLastMessage().getType() == EMMessage.Type.CUSTOM){
                    EMCustomMessageBody body = (EMCustomMessageBody) e.getValue().getLastMessage().getBody();
                    Map<String, String> params = body.getParams();
                    emap.put("content","custom");
                    if (params.containsKey("event")){
                        emap.put("event",params.get("event"));
                    }
                }else if (e.getValue().getLastMessage().getType() == EMMessage.Type.CMD){
                    emap.put("content","cmd");
                }
                lists.add(emap);
            }
        return lists;
    }

    @Override
    public int getUnreadMsgCount(String username) {
        ALog.d("username", "getUnreadMsgCount: " + username);
        EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username);
        if (conversation != null) {
            return conversation.getUnreadMsgCount();
        }
        return 0;
    }

    @Override
    public void cleanUnReadCount(String username) {
        EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username);
        //指定会话消息未读数清零
        conversation.markAllMessagesAsRead();
    }

    @Override
    public void init(Context context) {
    }

}
